import {Component} from '@angular/core';
import {NavController, NavParams,AlertController} from 'ionic-angular';

import request from 'superagent';
import {LoginPage} from '../tab-my/tab-my-login';
import {Backend} from '../../providers/backend';

@Component({
  templateUrl: 'build/pages/tab-resources/tab-resources-files.html'
})
export class Files {

  static get parameters() {
    return [[NavController], [NavParams], [Backend],[AlertController]];
  }

  constructor(navCtrl, navParams, backend,alertController) {
    this.id = navParams.get('id');
    this.thid = navParams.get('thid');
    
    this.tit=this.thid.replace( '+GTI+W',' GTI W' );
    this.group = navParams.get('group');
    console.log(this.id);
    this.navCtrl = navCtrl;
    this.navParams = navParams;
    this.alertCtrl=alertController;
    this.backend = backend;
    this.inTheaters = {};
    this.img1 = 'asset/img/1-1.png';
    this.menus = [];
    
     this.backend.meetinggroup(this.thid,this.group)
      .then(result => {
          console.log(result);
        this.resourcesWhitePaper = result;
        for(let i=0;i<this.resourcesWhitePaper.length;i++){
          this.backend.filesDownload(this.resourcesWhitePaper[i].id)
      .then(result => {
        console.log(result);
        this.resourcesWhitePaper[i].kb2=result.title;
        this.resourcesWhitePaper[i].menus = result;
      this.resourcesWhitePaper[i].kb1= Number(this.resourcesWhitePaper[i].menus.filesize.substring(0,this.resourcesWhitePaper[i].menus.filesize.length - 5))/1024
      this.resourcesWhitePaper[i].kb=Math.round(this.resourcesWhitePaper[i].kb1*100)/100;
        
      });
        }
        console.log(this.resourcesWhitePaper);
      });
    // this.backend.filesDownload(this.id)
    //   .then(result => {
    //     this.menus = result;
    //   this.kb1= Number(this.menus.filesize.substring(0,this.menus.filesize.length - 5))/1024
    //   this.kb=Math.round(this.kb1*100)/100;
    //     console.log(this.kb);
    //   });
  }

  doRefresh(refresher) {
      this.backend.meetinggroup(this.thid,this.group)
      .then(result => {
          console.log(result);
        this.resourcesWhitePaper = result;
        for(let i=0;i<this.resourcesWhitePaper.length;i++){
          this.backend.filesDownload(this.resourcesWhitePaper[i].id)
      .then(result => {
        console.log(result);
        this.resourcesWhitePaper[i].kb2=result.title;
        this.resourcesWhitePaper[i].menus = result;
      this.resourcesWhitePaper[i].kb1= Number(this.resourcesWhitePaper[i].menus.filesize.substring(0,this.resourcesWhitePaper[i].menus.filesize.length - 5))/1024
      this.resourcesWhitePaper[i].kb=Math.round(this.resourcesWhitePaper[i].kb1*100)/100;
        
      });
        }
        
      });
    setTimeout(() => {
      console.log('Async operation has ended');
      refresher.complete();
    }, 2000);
  }

  _loadById(id) {
    return new Promise((resolve) => {
      request
        .get('http://10.3.1.2/v2/movie/subject/' + id)
        .end((err, res) => {
          resolve(res.body);
        });
    });
  }
  download(path,group) { 
    console.log(group);
    
     this.backend.isLogined()
      .then(result => {
        this.result1=result;
        console.log(this.result1);
        if(this.result1.didTutorial == 'true'){
           this.backend.privileges(this.result1.groupid)
      .then(result => {
          console.log(result);
        this.privileges = result;
        if(this.privileges == 1){
          this.backend.fileDownload(path);
        }else{
          let alert = this.alertCtrl.create({
                title: 'NO AUTHORITY ',
                
                buttons: ['CLOSE']
              });
              alert.present();
        }
      });
            // if(this.result1.groupid==group){
            //   console.log(this.result1.groupid);
            //   console.log(this.group);
            //     this.backend.fileDownload(path);
            // }else{
             
            //     let alert = this.alertCtrl.create({
            //     title: 'NO AUTHORITY ',
                
            //     buttons: ['CLOSE']
            //   });
            //   alert.present();
            // }
          
        }else{
           
    this.navCtrl.rootNav.push(LoginPage);
          
        }
      });
    
   
  }

}

